import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
    return {
        token: getToken(),
        name: '',
        avatar: '',
        userId: '',
        roles: [],
        permissions: [],
        buttons: [], // 按钮权限
        deptId:"",
        userName:"",
        parentId:"",
        loginName:""
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
    SET_USERID: (state, userId) => {
        state.userId = userId
    },
    SET_ROLES: (state, roles) => {
        state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
        state.permissions = permissions
    },
    SET_BUTTONS: (state, buttons) => {
        state.buttons = buttons
    },
    SET_DEPTID: (state, deptId) => {
        state.deptId = deptId
    },
    SET_USERNAME :(state, userName) => {
        state.userName = userName
    },
    SET_PARENTID :(state, parentId) =>{
        state.parentId = parentId
    },
    SET_LOGINNAME :(state, loginName) => {
        state.loginName = loginName
    },
}

const actions = {
    // user login
    login({ commit }, userInfo) {
        const { username, password, captcha, randomStr } = userInfo
        return new Promise((resolve, reject) => {
            login({ username: username.trim(), password: password, captcha: captcha, randomStr: randomStr }).then(response => {
                const { data } = response
                commit('SET_TOKEN', data.token)
                commit('SET_USERID', data.userId)
                setToken(data.token)
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get user info
    getInfo({ commit, state }) {
        return new Promise((resolve, reject) => {
            getInfo(state.token).then(response => {
                const { data } = response
                if (!data) {
                    reject('Verification failed, please Login again.')
                }
                const { name, avatar, roleIds, userId, permissions ,buttons,deptId ,userName,parentId,loginName} = data
                commit('SET_ROLES', roleIds)
                commit('SET_NAME', name)
                commit('SET_AVATAR', avatar)
                commit('SET_USERID', userId)
                commit('SET_PERMISSIONS', permissions)
                commit('SET_BUTTONS', buttons)
                commit('SET_DEPTID', deptId)
                commit('SET_USERNAME', userName)
                commit('SET_PARENTID', parentId)
                commit('SET_LOGINNAME', loginName)
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // user logout
    logout({ commit, state }) {
        return new Promise((resolve, reject) => {
            logout(state.token).then(() => {
                removeToken() // must remove  token  first
                resetRouter()
                commit('RESET_STATE')
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // remove token
    resetToken({ commit }) {
        return new Promise(resolve => {
            removeToken() // must remove  token  first
            commit('RESET_STATE')
            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}

